home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / screendoor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.8 KB  |  195 lines

  1. #include <assert.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glut.h>
  6.  
  7. /* Some <math.h> files do not define M_PI... */
  8. #ifndef M_PI
  9. #define M_PI 3.14159265358979323846
  10. #endif
  11.  
  12. #ifndef __sgi
  13. #define trunc(x) ((double)((int)(x)))
  14. #endif
  15.  
  16. #ifdef _WIN32
  17. #define random() ((long)rand() + (rand() << 15) + (rand() << 30))
  18. #endif
  19.  
  20. GLUquadricObj *cone, *base, *qsphere;
  21.  
  22. void create_stipple_pattern(GLuint *pat, GLfloat opacity)
  23. {
  24.   int x, y;
  25.   long threshold = (float)0x7fffffff * (1. - opacity);
  26.  
  27.   for (y = 0; y < 32; y++) {
  28.     pat[y] = 0;
  29.     for (x = 0; x < 32; x++) {
  30.       if (random() > threshold) pat[y] |= (1 << x);
  31.     }
  32.   }
  33. }
  34.  
  35. void init(void)
  36. {
  37.   static GLfloat lightpos[] = {.5, .75, 1.5, 1};
  38.   GLuint spherePattern[32];
  39.  
  40.   glEnable(GL_DEPTH_TEST); 
  41.   glEnable(GL_LIGHTING);
  42.   glEnable(GL_LIGHT0);
  43.  
  44.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  45.  
  46.   cone = gluNewQuadric();
  47.   base = gluNewQuadric();
  48.   qsphere = gluNewQuadric();
  49.   gluQuadricOrientation(base, GLU_INSIDE);
  50.  
  51.   create_stipple_pattern(spherePattern, .5);
  52.   glPolygonStipple((GLubyte *)spherePattern);
  53. }
  54.  
  55. void reshape(GLsizei w, GLsizei h) 
  56. {
  57.   glViewport(0, 0, w, h);
  58.   
  59.   glMatrixMode(GL_PROJECTION);
  60.   glLoadIdentity();
  61.   gluPerspective(60, 1, .01, 10);
  62.   gluLookAt(0, 0, 2.577, 0, 0, -5, 0, 1, 0);
  63.   
  64.   glMatrixMode(GL_MODELVIEW);
  65.   glLoadIdentity();
  66. }
  67.  
  68. void draw_room(void)
  69. {
  70.   /* material for the walls, floor, ceiling */
  71.   static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f};
  72.  
  73.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
  74.  
  75.   glBegin(GL_QUADS);
  76.   
  77.   /* floor */
  78.   glNormal3f(0, 1, 0);
  79.   glVertex3f(-1, -1, 1);
  80.   glVertex3f(1, -1, 1);
  81.   glVertex3f(1, -1, -1);
  82.   glVertex3f(-1, -1, -1);
  83.  
  84.   /* ceiling */
  85.   glNormal3f(0, -1, 0);
  86.   glVertex3f(-1, 1, -1);
  87.   glVertex3f(1, 1, -1);
  88.   glVertex3f(1, 1, 1);
  89.   glVertex3f(-1, 1, 1);  
  90.  
  91.   /* left wall */
  92.   glNormal3f(1, 0, 0);
  93.   glVertex3f(-1, -1, -1);
  94.   glVertex3f(-1, -1, 1);
  95.   glVertex3f(-1, 1, 1);
  96.   glVertex3f(-1, 1, -1);
  97.  
  98.   /* right wall */
  99.   glNormal3f(-1, 0, 0);
  100.   glVertex3f(1, 1, -1);
  101.   glVertex3f(1, 1, 1);
  102.   glVertex3f(1, -1, 1);
  103.   glVertex3f(1, -1, -1);
  104.  
  105.   /* far wall */
  106.   glNormal3f(0, 0, 1);
  107.   glVertex3f(-1, -1, -1);
  108.   glVertex3f(1, -1, -1);
  109.   glVertex3f(1, 1, -1);
  110.   glVertex3f(-1, 1, -1);
  111.  
  112.   glEnd();
  113. }
  114.  
  115. void draw_cone(void)
  116. {
  117.   static GLfloat cone_mat[] = {0.f, .5f, 1.f, 1.f};
  118.  
  119.   glPushMatrix();
  120.   glTranslatef(0, -1, 0);
  121.   glRotatef(-90, 1, 0, 0);
  122.  
  123.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
  124.   gluCylinder(cone, .3, 0, 1.25, 20, 1);
  125.   gluDisk(base, 0., .3, 20, 1); 
  126.  
  127.   glPopMatrix();
  128. }
  129.  
  130. void draw_sphere(GLdouble angle)
  131. {
  132.   static GLfloat sphere_mat[] = {1.f, .5f, 0.f, 1.f};
  133.  
  134.   glPushMatrix();
  135.   glTranslatef(0, -.3, 0);
  136.   glRotatef(angle, 0, 1, 0);
  137.   glTranslatef(0, 0, .6);
  138.  
  139.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
  140.   gluSphere(qsphere, .3, 20, 20);
  141.  
  142.   glPopMatrix();
  143. }
  144.  
  145. GLdouble get_secs(void)
  146. {
  147.   return glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  148. }
  149.  
  150. void draw(void)
  151. {
  152.     GLenum err;
  153.     GLdouble secs;
  154.  
  155.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156.  
  157.     draw_room();
  158.     draw_cone();
  159.     secs = get_secs();
  160.  
  161.     /* draw the transparent object... */
  162.     glEnable(GL_POLYGON_STIPPLE);
  163.     draw_sphere(secs * 360. / 10.);
  164.     glDisable(GL_POLYGON_STIPPLE);
  165.  
  166.     err = glGetError();
  167.     if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));
  168.  
  169.     glutSwapBuffers();
  170. }
  171.  
  172. /* ARGSUSED1 */
  173. void key(unsigned char key, int x, int y)
  174. {
  175.   if (key == 27) exit(0);
  176. }
  177.  
  178. main(int argc, char *argv[])
  179. {
  180.     glutInit(&argc, argv);
  181.     glutInitWindowSize(256, 256);
  182.     glutInitWindowPosition(0, 0);
  183.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  184.     glutCreateWindow(argv[0]);
  185.     glutDisplayFunc(draw);
  186.     glutIdleFunc(draw);
  187.     glutKeyboardFunc(key);
  188.     glutReshapeFunc(reshape);
  189.     init();
  190.  
  191.     glutMainLoop();
  192.     return 0;
  193. }
  194.  
  195.